package bill.java.interview.amazon;

import java.util.LinkedList;

public class AmazonInterviewQuestions {

	public static void main(String[] str) {

		System.out.println(" testing 1:  " + numberToAlphabet(1));
		System.out.println(" testing 25:  " + numberToAlphabet(25));
		System.out.println(" testing 26:  " + numberToAlphabet(26));
		System.out.println(" testing 27:  " + numberToAlphabet(27));
		System.out.println(" testing 51:  " + numberToAlphabet(51));
		System.out.println(" testing 52:  " + numberToAlphabet(52));
		System.out.println(" testing 53:  " + numberToAlphabet(53));
		System.out.println(" testing 34567:  " + numberToAlphabet(34567));
		System.out.println(" testing 0:  " + numberToAlphabet(0));

	}

	public static String numberToAlphabet(int n) {
		if (n < 0) {
			throw new IllegalArgumentException(
					"The input can not be negative. ");
		}

		LinkedList<Character> stack = new LinkedList<Character>();

		if (n == 0)
			return "A";

		
		while (n / 26 >= 0 && n != 0) {
			char c = (char) (n % 26);
			stack.push(c);
			n = n / 26;
		}
		
		StringBuilder result = new StringBuilder();
		while (!stack.isEmpty()) {
			if (stack.size() == 1)
				result.append((char) (stack.pop() + 'A'));
			else
				result.append((char) (stack.pop() + 'A' - 1));
		}

		return result.toString();
	}

}
